package com.mall.controller.interceptor;


import com.mall.domain.MsAnnouncement;
import com.mall.domain.Page;
import com.mall.service.impl.AnnouncementService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器（公告controller）
 * </p>
 *
 * @since 2023-12-19
 */
@RestController
@RequestMapping("/announcement")
@RequiredArgsConstructor
public class AnnouncementController {

    @Resource
    AnnouncementService announcementService;

    @PostMapping("/listMsAnnouncement")
    public @ResponseBody
    Map<String, Object> listMsAnnouncement(@RequestBody Page page) {
        //放入Map集合
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        List<MsAnnouncement> msAnnouncements = announcementService.listMsAnnouncement(page);
        Integer total = announcementService.findTotal(page);
        metaMap.put("data", msAnnouncements);
        metaMap.put("total", total);
        metaMap.put("msg","获取分类列表成功");
        metaMap.put("status",200);
        return metaMap;
    }
    @PostMapping("/listMsAnnouncementById")
    public @ResponseBody
    Map<String, Object> listMsAnnouncementById(@RequestBody MsAnnouncement msAnnouncement) {
        //放入Map集合
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        MsAnnouncement msAnnouncements = announcementService.listMsAnnouncementById(msAnnouncement.getId());
        metaMap.put("data", msAnnouncements);
        metaMap.put("msg","获取列表成功");
        metaMap.put("status",200);
        return metaMap;
    }

    @PostMapping("/addMsAnnouncement")
    public @ResponseBody
    Map<String, Object> addMsAnnouncement(@RequestBody MsAnnouncement msAnnouncement) {
        Map<String,Object> metaMap = new HashMap<>();
        try {
            announcementService.addMsAnnouncement(msAnnouncement);
            metaMap.put("msg","添加成功");
            metaMap.put("status",200);
            return metaMap;
        }catch (Exception e){
            metaMap.put("status",100);
            metaMap.put("msg",e.getMessage());
            return metaMap;
        }
    }

    @PostMapping("/editMsAnnouncement")
    public @ResponseBody
    Map<String, Object> editMsAnnouncement(@RequestBody MsAnnouncement msAnnouncement) {
        Map<String,Object> metaMap = new HashMap<>();
        try {
            announcementService.editMsAnnouncement(msAnnouncement);
            metaMap.put("msg","成功");
            metaMap.put("status",200);
            return metaMap;
        }catch (Exception e){
            metaMap.put("status",100);
            metaMap.put("msg",e.getMessage());
            return metaMap;
        }
    }

    @PostMapping("/delMsAnnouncement")
    public @ResponseBody
    Map<String, Object> delMsAnnouncement(@RequestBody MsAnnouncement msAnnouncement) {
        Map<String,Object> metaMap = new HashMap<>();
        try {
            announcementService.delMsAnnouncement(msAnnouncement.getId());
            metaMap.put("msg","成功");
            metaMap.put("status",200);
            return metaMap;
        }catch (Exception e){
            metaMap.put("msg",e.getMessage());
            metaMap.put("status",100);
            return metaMap;
        }
    }
}
